$(function () {
    var uiContent = $('#uiContent');
    var prizeList = $('#unFetchPrizeList');
    var loadingDiv = $('#loadingDiv');

    var openId = $.getUrlParam('openId');
    if (openId) {
        $.wxJsApiConfig(function () {
            main();
        });
    } else {
        $.gotoErrorPage(uiContent);
    }

    // 主函数
    function main() {
        uiContent.show();
        loadingDiv.hide();
        var telephone = $('#search_input').val();
        if (telephone) {
            loadData(telephone, 0);
        }
    }

    // 监控输入框值的变化，如果达到11位，自动触发搜索
    $('#search_input').bind('input propertychange', function () {
        var telephone = $('#search_input').val();
        if (telephone && telephone.length == 11) {
            loadData(telephone, 0);
        } else {
            prizeList.empty();
        }
    });

    // 生成列表中的一行
    function generateNode(userPrize) {
        var node = '<div class="weui_media_box weui_media_text">' +
            '<h3 class="weui_media_title">{title}</h3>' +
            '<div>' +
            '<p class="weui_media_desc" style="font-size: 14px; color: #666;">{tips}</p>' +
            '<p class="weui_media_desc" style="margin-top: 15px; color: #cecece;">领取时间：{createTime}</p>' +
            '</div>' +
            '<ul class="weui_media_info" style="float: right; padding-bottom: 0;">' +
            '<li class="weui_media_info_meta">' +
            '<a onclick="closePage();" class="weui_btn weui_btn_mini weui_btn_default" style="font-size: 12px;">输入代码</a>' +
            '</li>' +
            '</ul>' +
            '<div style="clear: both;"></div>' +
            '</div>';

        var titleNode = '{prizeName}<span style="padding: 2px 5px; margin: 0 0 0 8px; font-weight: bold;">代码【{prizeCode}】</span>';

        var title = titleNode.replace('{prizeName}', userPrize.pushNews.prizeNumber + userPrize.pushNews.prize.name).replace('{prizeCode}', userPrize.telephone + ":" + userPrize.prizeCode);
        var expiredTime = '永远';
        if (userPrize.pushNews.prize.expiredDay) {
            expiredTime = new Date((userPrize.createTime + userPrize.pushNews.prize.expiredDay * 24 * 3600) * 1000).format('yyyy-MM-dd hh:mm')
        }
        return node.replace('{title}', title).replace('{tips}', userPrize.pushNews.prize.tips)
            .replace('{createTime}', new Date(userPrize.createTime * 1000).format('yyyy-MM-dd hh:mm'));
    }

    // 生成空内容页面
    function generateEmptyNode(url) {
        return '<div class="weui_msg">' +
            '<div class="weui_icon_area"><i class="weui_icon_safe_warn weui_icon_msg"></i></div>' +
            '<div class="weui_text_area"><h2 class="weui_msg_title">您还没有未领礼品</h2></div>' +
            '<div class="weui_opr_area"><p class="weui_btn_area"><a href="' + url + '" class="weui_btn weui_btn_primary">立即领礼品</a></p></div>' +
            '<div class="weui_extra_area"></div>';
    }

    // 加载数据
    function loadData(telephone, pageNum) {
        loadingDiv.show();
        $.restClient('/mobile/prize/getUnFetch/' + telephone + '/' + pageNum, null, function (data) {
            loadingDiv.hide();
            if (data && data.data) {
                if (data.data.content && data.data.content.length) {
                    if (pageNum == 0) {
                        prizeList.empty();
                    }
                    $.each(data.data.content, function (index, oneData) {
                        prizeList.append(generateNode(oneData));
                    });
                } else {
                    prizeList.append(generateEmptyNode($.howGetPrize))
                }

                if (data.data.last) {
                    $(document.body).destroyInfinite();
                } else {
                    $(document.body).infinite().on("infinite", function () {
                        loadData(telephone, pageNum + 1);
                    });
                }
            }
        });
    }

    // 搜索
    window.doSearch = function () {
        var telephone = $('#search_input').val();
        if (!telephone) {
            $.alert('请您务必输入要查询的手机号码');
            return;
        }
        loadData(telephone, 0);
    };

    window.gotoPrizePage = function () {
        console.log('gotoPrizePage');
        console.log($.getUrlParam('openId'));
        window.location.href = '/mobile/showPrize.html?time=' + (+new Date()) + '&openId=' + $.getUrlParam('openId');
    };

    window.closePage = function () {
        wx.closeWindow();
    }
});
